<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form ref="searchFormRef" :inline="true" :model="queryInfo.query">
        <el-form-item label="分类级别" prop="level">
          <el-select
            v-model="queryInfo.query.level"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
            @change="getSelectData"
          >
            <el-option label="一级分类" value="1"> </el-option>
            <el-option label="二级分类" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称【ID】" prop="code">
          <el-select
            v-model="queryInfo.query.code"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in selectData.product_type"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/product/ProductClassAdd')"
        >
          新增分类
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="proClassList" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column label="分类编号" prop="code"></el-table-column>
        <el-table-column label="分类名称" prop="name"></el-table-column>
        <el-table-column label="分类级别">
          <template slot-scope="scope">
            <span v-if="scope.row.level == 1">一级分类</span>
            <span v-if="scope.row.level == 2">二级分类</span>
          </template>
        </el-table-column>
        <el-table-column label="上级分类" prop="pname"> </el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'ProductClassEdit',
                  query: scope.row
                })
              "
            >
              编辑
            </el-link>
            &nbsp;
            <el-link type="danger" @click="removeProductClassById(scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取产品分类列表的参数对象
      queryInfo: {
        query: {
          level: '',
          code: ''
        },
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 20
      },
      proClassList: [],
      total: 0,
      selectData: [],
      //   控制新增对话框的显示与隐藏
      addDialogVisible: false
    }
  },
  created() {
    this.getSelectData()
    this.getListData()
  },
  methods: {
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/product/typeList',
        this.queryInfo
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取产品分类列表失败！')
      }
      this.proClassList = res.data.list
      this.total = res.data.total
    },
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product_type',
        level: this.queryInfo.query.level
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    // 根据id删除对应的产品信息
    async removeProductClassById(row) {
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该产品分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.post('/admin/product/delType', {
        code: row.code
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
